package com.wx.service.impl;

import com.wx.dto.CarQueryDTO;
import com.wx.dto.CollectionOrOrderDTO;
import com.wx.dto.ConditionGetCarDTO;
import com.wx.entity.Car;
import com.wx.entity.Favor;
import com.wx.entity.Order;
import com.wx.entity.User;
import com.wx.exception.BaseException;
import com.wx.mapper.CarMapper;
import com.wx.service.CarService;
import com.wx.vo.FavorVO;
import com.wx.vo.OrderQueryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CarServiceImpl implements CarService {

    @Autowired
    private CarMapper carMapper;

    /*搜索汽车*/
    @Override
    public List<Car> carList(CarQueryDTO carQueryDTO) {

        List<Car> cars = carMapper.carList(carQueryDTO);
        return cars;
    }


    /*添加汽车到收藏*/
    @Override
    public void usercollection(CollectionOrOrderDTO collectionOrOrderDTO) {
        //同一辆车只能收藏一次
        Favor favor1 = carMapper.queryCollectionByCarNo(collectionOrOrderDTO);
        if(favor1 != null){
            throw new BaseException("请勿重复收藏");
        }

        //收藏汽车
        carMapper.addCollection(collectionOrOrderDTO);
    }


    /*添加汽车到订单*/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void order(CollectionOrOrderDTO collectionOrOrderDTO) {
        //赋值同名属性
        Order order = new Order();
        BeanUtils.copyProperties(collectionOrOrderDTO,order);
        //补全属性
        order.setIsFinish(0);
        order.setAddDate(LocalDateTime.now());

        //查询汽车数量是否大于0
        Long amount = carMapper.queryCarByCarNo(order.getCarNo());
        if(amount <= 0){
            throw new BaseException("汽车已售空");
        }

        //汽车数量减1
        carMapper.decCarAmount(order.getCarNo());

        //添加订单
        carMapper.addOrder(order);

    }

    /*获取用户收藏*/
    @Override
    public List<FavorVO> getcollection(CollectionOrOrderDTO collectionOrOrderDTO) {
        List<FavorVO> favors = carMapper.getcollection(collectionOrOrderDTO);
        return favors;
    }

    /*用户取消收藏*/
    @Override
    public void infavor(User user) {
        carMapper.infavor(user.getId());
    }

    /*获取用户订单*/
    @Override
    public List<OrderQueryVO> getOrder(User user) {
        List<OrderQueryVO> order = carMapper.getOrder(user);
        return order;
    }

    /*用户取消订单*/
    @Override
    public void delorder(User user) {
        carMapper.delorder(user.getId());
    }

    /*提交查询汽车*/
    @Override
    public List<Car> getcarcondition(ConditionGetCarDTO conditionGetCarDTO) {
        if(!conditionGetCarDTO.getPriceD().equals('0') && !conditionGetCarDTO.getPriceU().equals('0')){
            //String转Long类型
            conditionGetCarDTO.setPriceDown(Long.parseLong(conditionGetCarDTO.getPriceD())*10000);
            conditionGetCarDTO.setPriceUp(Long.parseLong(conditionGetCarDTO.getPriceU())*10000);
        }

        List<Car> car = carMapper.carCondition(conditionGetCarDTO);
        return car;
    }
}
